package turritopsis

import (
    "adkr/pkg/adkr"
)

// NetRouter is a minimal network abstraction for external injection.
// Cloud/cluster deployments should implement this to wire ADKR (and optional ACS) traffic.
// The goal is to avoid importing any concrete transport here (gRPC, TCP, NATS, etc.).
type NetRouter interface {
    // KRChannels returns the per-epoch channels for KeyRefresh traffic.
    // The implementation can multiplex by (epoch, tag, round) if desired; we only require one pair.
    KRChannels(epoch int) (<-chan adkr.Message, chan<- adkr.Message)

    // Optional: ACSChannels for ACS traffic (if you decide to run ACS via this router as well).
    // Returning nils means ACS is not wired through this router.
    ACSChannels(epoch int) (<-chan adkr.Message, chan<- adkr.Message)
}

// NetKRBuilder adapts a NetRouter into KRBuilder.
// Use together with MakeSingleRoundKRViaHigh(...) or NewKeyRefreshFuncUsingChannels(...).
func NetKRBuilder(router NetRouter, makeKR func(in <-chan adkr.Message, out chan<- adkr.Message) KeyRefreshFunc) func(int, EpochPlan) KeyRefreshFunc {
    return func(epoch int, _ EpochPlan) KeyRefreshFunc {
        in, out := router.KRChannels(epoch)
        if in == nil || out == nil { 
            // Debug: log why channels are nil
            return nil 
        }
        if makeKR == nil { 
            // Debug: log why makeKR is nil
            return nil 
        }
        krFunc := makeKR(in, out)
        if krFunc == nil {
            // Debug: log why makeKR returned nil
        }
        return krFunc
    }
}





